package com.example.project_863.service.impl;

import com.example.project_863.entity.DeviceDetails;
import com.example.project_863.entity.Record;
import com.example.project_863.mapper.DeviceMapper;
import com.example.project_863.mapper.RecordMapper;
import com.example.project_863.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    DeviceMapper deviceMapper;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public int addBatch() {
        List<DeviceDetails> deivces = deviceMapper.findAll();
        //保存最新每个设备的状态信息
        List<Record> records=new ArrayList();
        Random random=new Random();
        for (DeviceDetails details:deivces
        ) {
            Record record = new Record();
        //设置记录中的设备id
            record.setDevice_id(details.getId());
        //设置风机状态
            int i1 = random.nextInt(10);//0-9
            if(i1<9){
                details.setFan_state(1);
            }else{
                details.setFan_state(0);
            }
        //将风机状态同步到记录对象中
            record.setFan_state(details.getFan_state());
        //设置净化器状态
            int i2 = random.nextInt(10);//0-9
            if(i2<9){
                details.setPurify_state(1);
            }else{
                details.setPurify_state(0);
            }
        //将净化器状态，铜鼓到记录对象中
            record.setPurify_state(details.getPurify_state());
        //设置传感器状态
            int i3 = random.nextInt(10);//0-9
            if(i3<9){
                details.setSensor_state(1);
            }else{
                details.setSensor_state(0);
            }
        //将传感器状态同步到记录对象中
            record.setSensor_state(details.getSensor_state());
        //更新警告状态
            if(details.getFan_state()==1 && details.getPurify_state()==1 &&
                    details.getSensor_state()==1){
                details.setAlarm_state(1);
            }else{
                details.setAlarm_state(0);
            }
            try{
                if(details.getUpdate_time()!=null &&! details.getUpdate_time().equals(""))
                {
                    long l = (new Date().getTime() -
                            sdf.parse(details.getUpdate_time()).getTime())/1000;
                    if(l>60){
                        details.setAlarm_state(2);
                    }
                }
            }catch (Exception e){
                System.out.println("日期转换异常");
            }
            //设置错误日志
            String error_log="";
            if(details.getFan_state()==0){
                error_log=error_log+"风机故障、";
            }
            if(details.getPurify_state()==0){
                error_log=error_log+"净化器故障、";
            }
            if(details.getSensor_state()==0){
                error_log=error_log+"传感器故障、";
            }
            if(details.getAlarm_state()==2){
                error_log=error_log+"设备信息发送异常、";
            }
            if(!error_log.equals("")){
            //移除最后一个顿号
                error_log=error_log.substring(0,error_log.length()-1);
            }
            details.setError_log(error_log);
            //设置修改时间
            details.setUpdate_time(sdf.format(new Date()));
            //设置记录添加时间
            record.setCreate_time(details.getUpdate_time());
            //设置记录的油烟浓度
            double vocs = random.nextInt(100)/100.0;
            record.setVocs(vocs);
            //模拟60秒没有发送信息
            int i4 = random.nextInt(4);
            if(i4==0){
            //修改设备信息
                int res=deviceMapper.updateById(details);
            //将记录添加到记录列表中
                records.add(record);
            }
        }
            //向记录表中批量添加数据
        int res=recordMapper.addBatch(records);
        return res;
    }
    @Override
    public List<Map> findErrorLog() {
        return recordMapper.findErrorLog();
    }
    @Override
    public Map countRecord(String alarmType) {
        if(alarmType==null || alarmType.equals("")){
            alarmType="None";
        }
        return recordMapper.countRecord(alarmType);
    }
    @Override
    public List<Map> findAreaVocs() {
        return recordMapper.findAreaVocs();
    }
}
